package com.bruip.spring.service.impl;

import com.bruip.spring.dao.BookDao;
import com.bruip.spring.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 王若龙
 * @CreateTime 2022-09-03 11:22
 */
@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookDao bookDao;

    @Override
//    @Transactional(
//            readOnly = true
//            timeout = 3
//            noRollbackFor = ArithmeticException.class
//            isolation = Isolation.DEFAULT
//            propagation = Propagation.REQUIRES_NEW
//    )
    public void buyBook(Integer userId, Integer bookId) {
//        try {
//            TimeUnit.SECONDS.sleep(3);
//        }catch (Exception e){
//            e.printStackTrace();
//        }

        //查询图书的价格
        Integer price = bookDao.getPriceByBookId(bookId);
        //更新图书库存
        bookDao.updateStock(bookId);
        //更新用户的余额
        bookDao.updateBalance(userId, price);

//        System.out.println(1/0);

    }
}
